//
//  UIWindow.cpp
//  CocosLib
//
//  Created by wangdi on 13-8-1.
//
//

#include "UIWindow.h"

using namespace cocos2d;
namespace storm
{
	UIWindow *UIWindow::s_pInstance = NULL;

    void UIWindow::onEnter()
    {
        cocos2d::CCLayer::onEnter();
        cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
    }
    void UIWindow::onExit()
    {
        cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
        cocos2d::CCLayer::onExit();
    }
    
    void UIWindow::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
    {
        if(!isVisible() || !m_bEnable || !pTouches||!pTouches->count())
        {
            return;
        }
        
        if(NULL != s_pFocusWidget)
        {
            return;
        }
        
        cocos2d::CCSetIterator iter;
        for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
        {
            CCTouch *pCurTouch = (CCTouch *)(*iter);
            if(FindTouchReveiver(pCurTouch))
            {
                m_pCurTouch = pCurTouch;
                break;
            }
        }
        
    }
    void UIWindow::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
    {
        if(!pTouches||!pTouches->count())
        {
            return;
        }
        
        if(NULL == m_pCurTouch || NULL == s_pFocusWidget)
        {
            return;
        }
        
        cocos2d::CCSetIterator iter;
        for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
        {
            if(*iter == m_pCurTouch)
            {
                s_pFocusWidget->onCurTouchMoving(m_pCurTouch);
                 
                if(s_pFocusWidget != NULL && !s_pFocusWidget->isContainsTouch((cocos2d::CCTouch *)(*iter)))
                {
                    s_pFocusWidget->onCurTouchMoveOut(m_pCurTouch);
                }
            }
        }
    }
    void UIWindow::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
    {
        if(NULL == m_pCurTouch || NULL == s_pFocusWidget)
        {
            return;
        }
        cocos2d::CCSetIterator iter;
        for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
        {
            if(*iter == m_pCurTouch)
            {
                s_pFocusWidget->onCurTouchFinish(m_pCurTouch);
                m_pCurTouch = NULL;
                s_pFocusWidget = NULL;
            }
        }
    }
    void UIWindow::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
    {
        if(NULL == m_pCurTouch)
        {
            return;
        }
        cocos2d::CCSetIterator iter;
        for(iter = pTouches->begin(); iter != pTouches->end(); iter++)
        {
            if(*iter == m_pCurTouch)
            {
                
                s_pFocusWidget->onCurTouchFinish(m_pCurTouch);
                m_pCurTouch = NULL;
                s_pFocusWidget = NULL;
            }
        }
    }
    
    bool UIWindow::FindTouchReveiver(cocos2d::CCTouch *pTouch)
    {
        s_pFocusWidget = NULL;
        for (PanelList::iterator iter = m_listPanel.begin(); iter != m_listPanel.end(); iter++)
        {
            if((s_pFocusWidget = (*iter)->onReceiveNewTouch(pTouch))!= NULL)
            {
                return true;
            }
        }
        
        return false;
    }
  
    UIWindow::UIWindow()
        : m_pCurTouch(NULL)
    {
        s_pInstance = this;
    }
    
    UIWindow::~UIWindow()
    {
		if(s_pInstance == this) s_pInstance = NULL;
    }
    
    void UIWindow::AddPanel(storm::UIPanel *pPanel, int zOrder)
    {
        assert(pPanel != NULL);
        PanelList::iterator iter = m_listPanel.begin();
        while (iter != m_listPanel.end())
        {
            if((*iter)->getZOrder() < zOrder)
                break;
            iter++;
        }
        
        m_listPanel.insert(iter, pPanel);
        addChild(pPanel);
    }

	void UIWindow::RemovePanel(UIPanel *pPanel)
	{
		m_listPanel.remove(pPanel);
		pPanel->removeFromParentAndCleanup(true);
	}
    
}
